import { Notification } from '@douyinfe/semi-ui';

/**
 * 转换秒数为时分秒格式
 * @param {Number} seconds 
 * @returns 
 */
export function convertSecondsToHMS(seconds) {
    const h = Math.floor(seconds / 3600);
    const m = Math.floor((seconds % 3600) / 60);
    const s = seconds % 60;
    return `${h}h${m}m${s}s`;
}

/**
 * 通知请求错误弹窗
 * @param {String} tip 
 * @returns 
 */
export function notificationCatchErr(tip = '请求错误') {
    return (err) => Notification.error({
        title: tip,
        content: err.message,
        position: 'topRight'
    })
}

/**
 * 获取直播时长
 * 
 * @param {Object} row 
 * @returns number
 */
export function getLiveDuration(row) {
    if (row['finish_time'] === null) {
        return 0
    }

    return (new Date(row['finish_time']) - new Date(row['create_time'])) / 1000
}

/**
 * 转换为浮点数
 * @param {any} data 
 * @param {number} precision 
 * @returns 
 */
export function toFloat(data, precision = 2) {
    data = Number(data).toFixed(precision)
    return isNaN(data) ? 0 : data
}

/**
 * 生成头像图片地址
 * @param {String|null|undefined} avatar 
 * @returns 
 */
export function avatarUrl(avatar) {
    if (!avatar) return null;
    return '//coding-eps-dashboard.xiusin.cn/' + avatar
}